<template>
  <el-dialog title="绑定主体" v-model="openBind" width="1000px" append-to-body :close-on-click-modal="false" @close="cancel">
    <el-form ref="formBindRef" :model="formBind" :rules="rulesBind" label-width="120px">
      <el-form-item label="公司名称">
        <span v-if="subject_name">{{ subject_name }}</span>
        <span v-else>未绑定</span>
      </el-form-item>
      <el-table v-loading="loading" :data="subject_list">
        <el-table-column label="公司名称" align="center" prop="subjectName" />
        <el-table-column label="公司状态" align="center" prop="subjectStatus">
          <template #default="scope">
            <dict-tag :options="subject_status" :value="scope.row.subjectStatus" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button :type="scope.row.checked == true ? 'danger' : 'primary'" @click="handleCheck(scope.row)">
              <span>
                {{ scope.row.checked ? "取消选择" : "选择" }}
              </span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { getStation, updateStation } from "@/api/equipment/station";
import { listSubject } from "@/api/subject/subject";

const { proxy } = getCurrentInstance();

const { subject_status } = proxy.useDict("subject_status");

const loading = ref(false)
const openBind = ref(false)
const formBind = ref({
  subjectId: null,
})

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: null,
})

const rulesBind = ref({})
const subject_list = ref([])
const subject_name = ref(null)


function open(row) {
  const id = row.stationId || ids.value;
  getStation(id).then((response) => {
    formBind.value = response.data;
    openBind.value = true;
  });
  getSubject();
}

// 暴露出去
defineExpose({
  open,
})

function cancel() {
  openBind.value = false;
  reset();
}

function reset() {
  formBind.value = {};
  proxy.resetForm("formBindRef");
}

function getSubject() {
  loading.value = true;
  listSubject(queryParams.value).then((response) => {
    subject_list.value = response.data;
    if (subject_list.value.length > 0) {
      for (let i = 0; i < subject_list.value.length; i++) {
        if (formBind.value.subjectId == subject_list.value[i].subjectId) {
          subject_name.value = subject_list.value[i].subjectName;
          subject_list.value[i].checked = true;
        } else {
          subject_list.value[i].checked = false;
        }
      }
    }
    loading.value = false;
  });
}


function handleCheck(row) {
  if (formBind.value.subjectId == row.subjectId) {
    subject_name.value = null;
    formBind.value.subjectId = -1;
    row.checked = false;
  } else {
    subject_name.value = row.subjectName;
    formBind.value.subjectId = row.subjectId;
    row.checked = true;
  }
  getSubject();
}


function submitForm() {
  proxy.$refs["formBindRef"].validate((valid) => {
    if (valid) {
      updateStation(formBind.value).then((response) => {
        if (response.code == 0) {
          proxy.$modal.notifySuccess("绑定成功");
          cancel();
          if (reload.value) reload();
        }
      });
    }
  });
}
</script>
